package com.example.dell.fastshopping.product;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.dell.fastshopping.R;
import com.example.dell.fastshopping.utils.GlideUtils;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.loader.ImageLoaderInterface;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by ${YSH} on 2019/5/28.
 * 描述        ${TODO}
 * <p>
 * 更新者       $author$
 * 更新时间     $Data$
 * 描述         ${TODO}
 */
public class ProductRightFragment extends Fragment {
    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.banner)
    Banner banner;
    private Unbinder bind;
    private ProductModel model;
    private Context context;
    private List<Product> mList;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_product_right, container, false);
        bind = ButterKnife.bind(this, inflate);
        return inflate;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        model = ViewModelProviders.of(getActivity()).get(ProductModel.class);
        initView();
    }

    private void initView() {

        rv.setLayoutManager(new GridLayoutManager(context, 3));
        model.getSelect().observe(this, new Observer<ProductMenu>() {
            @Override
            public void onChanged(@Nullable ProductMenu productMenu) {
                setData(productMenu);
            }
        });
        model.getProductBanner().observe(this, new Observer<List<BannerEntity>>() {
            @Override
            public void onChanged(@Nullable List<BannerEntity> bannerEntities) {
                setBanner(bannerEntities);
            }
        });
    }

    private void setData(ProductMenu productMenu) {
        model.getProduct(productMenu.getValue()).observe(this, new Observer<List<Product>>() {
            @Override
            public void onChanged(@Nullable List<Product> products) {
                mList = products;
                RecyclerView.Adapter adapter = rv.getAdapter();
                if (adapter == null) {
                    rv.setAdapter(new MyAdapter());
                } else {
                    adapter.notifyDataSetChanged();
                }
            }
        });



    }

    private void setBanner(List<BannerEntity> list) {
        //设置图片网址或地址的集合
        banner.setImages(list);
        //设置轮播间隔时间
        banner.setDelayTime(3000);
        //设置是否为自动轮播，默认是“是”。
        banner.isAutoPlay(true);
        banner.setBannerStyle(BannerConfig.CENTER);
        //设置轮播的动画效果，内含多种特效，可点入方法内查找后内逐一体验
        banner.setBannerAnimation(Transformer.Default);
        banner.setImageLoader(new ImageLoaderInterface() {
            @Override
            public void displayImage(Context context, Object object, View imageView) {
                BannerEntity entity = (BannerEntity) object;
                Glide.with(context).load(entity.getPic()).centerCrop().into((ImageView) imageView);
            }

            @Override
            public View createImageView(Context context) {
                return null;
            }
        });
        banner.start();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        bind.unbind();
    }

    private class MyAdapter extends RecyclerView.Adapter<MyHolder> {
        @NonNull
        @Override
        public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View inflate = LayoutInflater.from(context).inflate(R.layout.item_product_right, viewGroup, false);
            return new MyHolder(inflate);
        }

        @Override
        public void onBindViewHolder(@NonNull MyHolder myHolder, int i) {
            Product product = mList.get(i);
            myHolder.title.setText(product.getLabel());
            GlideUtils.setView(context, product.getPic(), myHolder.pic);
        }

        @Override
        public int getItemCount() {
            return mList.size();
        }
    }

    private class MyHolder extends RecyclerView.ViewHolder {

        public TextView title;
        public ImageView pic;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            pic = itemView.findViewById(R.id.pic);
            title = itemView.findViewById(R.id.title);
        }
    }
}
